﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using System.ServiceModel;
using Contracts;


namespace Presentation
{
 
    public partial class Teacher_List : System.Web.UI.Page
    {
        private IProjectBService channelObject;

        public Teacher_List()
        {
            ChannelFactory<IProjectBService> ChannelFactory = new ChannelFactory<IProjectBService>("ProjectBService");
            channelObject=ChannelFactory.CreateChannel();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    List<Teacher> showResult = new List<Teacher>();
                    if (channelObject != null)
                    {
                        showResult = channelObject.GetAllTeacher();
                        Bind(GridView1, showResult);
                    }
                }


                catch(Exception ex)
                {
                    Response.Write(ex.Message);
                }


            }
        }

        private void Bind(GridView GV, List<Teacher> table)
        {
            GV.DataSource = table;
            GV.DataBind();
        }

        protected void Button_Create_Click(object sender, EventArgs e)//call create or update page
        {
           // Response.Redirect("Create.aspx");//.............................................................................problem
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)//call update and delete 
        {
            if ("Update" == e.CommandName)
            {
                string hid = e.CommandArgument.ToString();
                Response.Redirect("Update.aspx?id=" + hid); 
            }

            if ("DeleteRow" == e.CommandName)
            {
                string hid = e.CommandArgument.ToString();
                int teacherid = Convert.ToInt32(hid);
                List<Teacher> showResult = new List<Teacher>();
                Teacher teacher = new Teacher()
                {
                    TeacherId = teacherid
                };
                showResult = channelObject.GetTeacher(teacher);

                foreach (var a in showResult)
                {
                   
                    List<Department> departmentList = new List<Department>();
                    Department de = new Department()
                    {
                        DepartmentId = a.DepartMent.DepartmentId
                    };
                    departmentList = channelObject.GetDepartment(de);
                   
                    Department myDep = new Department();
                    foreach (var p in departmentList)
                    {
                        myDep = p;
                    }
                    

                    Teacher temp =new Teacher
                    {
                         TeacherId = a.TeacherId,
                         TeacherName = a.TeacherName,
                         TeacherGender = a.TeacherGender,
                         DepartMent = myDep
                    };

                    if (channelObject.DeleteTeacher(temp))
                    {
                        Response.Write("<script>alert('Delete Succeed');window.location.href='TeacherList.aspx';</script>");
                    }
                    else
                        Response.Write("<script>alert('Delete Failed');windows.location.href='TeacherList.aspx';</script>");
                    Response.End();
                }
               
            }
        }
    }
}